package com.yhh;

import com.yhh.Entity.Command;
import com.yhh.Entity.HandlerCommand;
import com.yhh.Exceptions.InstructionException;
import com.yhh.Exceptions.LocalCommandException;
import com.yhh.Exceptions.NonInstructionException;

import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class HelpCommand extends Command implements HandlerCommand {
    public HelpCommand(Socket socket) {
        super(socket);
        command.add("-a");
        command.add("-h");

        map.put("send", "send -f filename [-d] [counts] [-t] [d TimeUnit]\n-f为强制参数，后接绝对路径文件名；-d为可选参数，表示该文件被下载几次后删除；-t表示过期时间，接数字加单位");
        map.put("recv", "recv -m key\n-m为强制参数，后接此次传输的密钥");
        map.put("show", "show 打印你已上传的文件");
        map.put("clear", "clear [-a] 全部清除 | [-m key] 清除对应key的文件");
        map.put("help", "help -a|-h [command]");
    }

    private HashMap<String, String> map = new HashMap<>();

    int flag = 1;

    @Override
    public void handlerParameter(Map<String, String> args) throws InstructionException{
        if (args.get("-a") != null) {
            flag = 0;
            return;
        }
        if (args.get("-h") == null) {
            throw new InstructionException("help需要一个-h或-a参数", "help");
        }
        flag = 1;
    }

    @Override
    public boolean check(Map<String, String> args) throws InstructionException {
        //处理指令参数
        checkParameter(args);
        handlerParameter(args);
        return true;
    }

    @Override
    public void handlerCommand(Map<String, String> args) throws LocalCommandException {
        if (flag == 0) {
            map.forEach((k, v) -> {
                System.out.print("["+k+"]");
                System.out.print(" ");
                System.out.println(v);
            });
        } else if (flag == 1) {
            System.out.println(map.get(args.get("-h")));
        }
        throw new LocalCommandException();
    }

}
